﻿using System;
using System.Threading;
using UnityEditor;
using UnityEngine;

public class TrackItemControl
{
    protected int controlID;
    protected Rect controlPosition;
    private int drawPriority;
    private TimelineTrackWrapper track;
    private TimelineTrackControl trackControl;
    private TimelineItemWrapper wrapper;

    public event TrackItemEventHandler AlterTrackItem;

    protected void copyItem(object userData)
    {
        Behaviour behaviour = userData as Behaviour;
        if (behaviour != null)
        {
            DirectorCopyPaste.Copy(behaviour);
        }
    }

    protected void deleteItem(object userData)
    {
        Behaviour behaviour = userData as Behaviour;
        if (behaviour != null)
        {
            Undo.DestroyObjectImmediate(behaviour.gameObject);
        }
    }

    public virtual void Draw(DirectorControlState state)
    {
        Behaviour behaviour = this.wrapper.Behaviour;
        if (behaviour != null)
        {
            Color color = GUI.color;
            bool flag = false;
            foreach (GameObject obj2 in Selection.gameObjects)
            {
                if (obj2 == behaviour.gameObject)
                {
                    flag = true;
                }
            }
            if (flag)
            {
                GUI.color = new Color(0.5f, 0.6f, 0.905f, 1f);
            }
            GUI.Box(this.controlPosition, GUIContent.none, TimelineTrackControl.styles.EventItemStyle);
            GUI.color = color;
            Rect position = new Rect(this.controlPosition.x + 16f, this.controlPosition.y, 128f, this.controlPosition.height);
            GUI.Label(position, behaviour.name);
        }
    }

    public virtual void HandleInput(DirectorControlState state, Rect trackPosition)
    {
        Behaviour objectToUndo = this.wrapper.Behaviour;
        float num = (this.wrapper.Firetime * state.Scale.x) + state.Translation.x;
        this.controlPosition = new Rect(num - 8f, 0f, 16f, trackPosition.height);
        this.controlID = GUIUtility.GetControlID(this.wrapper.Behaviour.GetInstanceID(), FocusType.Passive, this.controlPosition);
        switch (Event.current.GetTypeForControl(this.controlID))
        {
            case EventType.MouseDown:
            {
                if (!this.controlPosition.Contains(Event.current.mousePosition) || (Event.current.button != 0))
                {
                    goto Label_0167;
                }
                GUIUtility.hotControl = this.controlID;
                if (!Event.current.control)
                {
                    Selection.activeInstanceID = objectToUndo.GetInstanceID();
                    break;
                }
                bool flag = false;
                foreach (GameObject obj2 in Selection.gameObjects)
                {
                    if (obj2 == objectToUndo.gameObject)
                    {
                        flag = true;
                    }
                }
                if (!flag)
                {
                    GameObject[] gameObjects = Selection.gameObjects;
                    ArrayUtility.Add<GameObject>(ref gameObjects, objectToUndo.gameObject);
                    Selection.objects = gameObjects;
                }
                else
                {
                    GameObject[] array = Selection.gameObjects;
                    ArrayUtility.Remove<GameObject>(ref array, objectToUndo.gameObject);
                    Selection.objects = array;
                }
                break;
            }
            case EventType.MouseUp:
                if (GUIUtility.hotControl == this.controlID)
                {
                    GUIUtility.hotControl = 0;
                    if (this.AlterTrackItem != null)
                    {
                        this.AlterTrackItem(this, new TrackItemEventArgs(this.wrapper.Behaviour, this.wrapper.Firetime));
                    }
                }
                goto Label_0250;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == this.controlID)
                {
                    Undo.RecordObject(objectToUndo, string.Format("Changed {0}", objectToUndo.name));
                    float time = (Event.current.mousePosition.x - state.Translation.x) / state.Scale.x;
                    this.wrapper.Firetime = state.SnappedTime(time);
                }
                goto Label_0250;

            default:
                goto Label_0250;
        }
        Event.current.Use();
    Label_0167:
        if (this.controlPosition.Contains(Event.current.mousePosition) && (Event.current.button == 1))
        {
            this.showContextMenu(objectToUndo);
            Event.current.Use();
        }
    Label_0250:
        if (Selection.activeGameObject == objectToUndo.gameObject)
        {
            if ((Event.current.type == EventType.ValidateCommand) && (Event.current.commandName == "Copy"))
            {
                Event.current.Use();
            }
            if ((Event.current.type == EventType.ExecuteCommand) && (Event.current.commandName == "Copy"))
            {
                DirectorCopyPaste.Copy(objectToUndo);
                Event.current.Use();
            }
        }
        if (((Event.current.type == EventType.KeyDown) && (Event.current.keyCode == KeyCode.Delete)) && (Selection.activeGameObject == objectToUndo.gameObject))
        {
            this.deleteItem(objectToUndo);
            Event.current.Use();
        }
    }

    public virtual void Initialize(TimelineItemWrapper wrapper, TimelineTrackWrapper track)
    {
        this.wrapper = wrapper;
        this.track = track;
    }

    public virtual void PostUpdate(DirectorControlState state)
    {
    }

    public virtual void PreUpdate(DirectorControlState state, Rect trackPosition)
    {
    }

    protected virtual void showContextMenu(Behaviour behaviour)
    {
        GenericMenu menu = new GenericMenu();
        menu.AddItem(new GUIContent("Copy"), false, new GenericMenu.MenuFunction2(this.copyItem), behaviour);
        menu.AddSeparator(string.Empty);
        menu.AddItem(new GUIContent("Clear"), false, new GenericMenu.MenuFunction2(this.deleteItem), behaviour);
        menu.ShowAsContext();
    }

    public int DrawPriority
    {
        get
        {
            return this.drawPriority;
        }
        set
        {
            this.drawPriority = value;
        }
    }

    public TimelineTrackWrapper Track
    {
        get
        {
            return this.track;
        }
        set
        {
            this.track = value;
        }
    }

    public TimelineTrackControl TrackControl
    {
        get
        {
            return this.trackControl;
        }
        set
        {
            this.trackControl = value;
        }
    }

    public TimelineItemWrapper Wrapper
    {
        get
        {
            return this.wrapper;
        }
        set
        {
            this.wrapper = value;
        }
    }
}

